package com.heima.wemedia.listen;

import com.alibaba.fastjson.JSON;
import com.heima.common.constants.UserConstants;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.wemedia.service.WmUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author 谢金成
 * @date 2024/4/27 18:43
 */
@Component
@Slf4j
public class AddWemediaUserListener {

    @Autowired
    private WmUserService wmUserService;

    @KafkaListener(topics = UserConstants.ADD_WEMEDIA_USER_TOPIC)
    public void onMessage(String msg) {
        if (StringUtils.isNotBlank(msg)) {
            ApUser apUser = JSON.parseObject(msg, ApUser.class);
            if (apUser != null) {
                // 封装
                WmUser wmUser = new WmUser();
                BeanUtils.copyProperties(apUser, wmUser);
                wmUser.setCreatedTime(new Date());
                wmUser.setApUserId(apUser.getId());
                wmUser.setStatus(9);
                wmUserService.save(wmUser);
            }
        }
    }
}
